<?php
class task_list extends Model
{
    public $programme_id;
	public $type;
	public $action;
	public $relational_id;
	public $push_time;
	public $modified_time;

    public static function getTableName()
    {
        return 'task_list';
    }

    public  function getPrimaryKey()
    {
        return array('type', 'relational_id');
    }

    public function set($obj) 
    {
        foreach (get_object_vars($this) as $field => $value) {
            $this->$field = isset($obj->$field) ? $obj->$field : '';
        }
    }

    public function findOne($id = NULL)
    {
        if($id){
			$data = self::$dbo['portal_check_video_r']->findInstance($this->getTableName(),$id);
		}else{
			$data =  self::$dbo['portal_check_video_r']->find($this->getTableName());
		}
		return $data;
    }

    public function insert($obj)
    {
        return Model::$dbo['portal_check_video_w']->insert($obj);
    }

    public function update($obj)
    {
       return Model::$dbo['portal_check_video_w']->save($obj);
    }

    public function delete($obj)
    {
        return Model::$dbo['portal_check_video_w']->delete(__CLASS__, 'type=? AND relational_id=?', array($obj->type, $obj->relational_id));
    }

    public function getTaskByProgrammeId($programme_id)
    {
        return Model::$dbo['portal_check_video_r']->find(__CLASS__, 'programme_id=?', array($programme_id));
    }

	public static function getTaskCount($programme_id)
	{
		$sql = '
			SELECT COUNT(*) AS count
			FROM ' . self::getTableName() . '
			WHERE programme_id = ' . $programme_id;
		$ret = self::$dbo['portal_check_video_r']->findBySql(__CLASS__, $sql);
		return empty($ret[0]->count) ? 0 : $ret[0]->count;
	}
}
?>
